#!/bin/bash

set -e

[ -z "$PLYMOUTH_LIBEXECDIR" ] && PLYMOUTH_LIBEXECDIR="@PLYMOUTH_LIBEXECDIR@"
[ -z "$PLYMOUTH_DATADIR" ] && PLYMOUTH_DATADIR="@PLYMOUTH_DATADIR@"
[ -z "$PLYMOUTH_CONFDIR" ] && PLYMOUTH_CONFDIR="@PLYMOUTH_CONF_DIR@"
[ -z "$PLYMOUTH_POLICYDIR" ] && PLYMOUTH_POLICYDIR="@PLYMOUTH_POLICY_DIR@"
if [ -z "$PLYMOUTH_PLUGIN_PATH" ]; then
    if [ -z "$LIB" ]; then
        PLYMOUTH_PLUGIN_PATH="$(plymouth --get-splash-plugin-path)"
    else
        [ -z "$PLYMOUTH_LIBDIR" ] && PLYMOUTH_LIBDIR="@PLYMOUTH_LIBDIR@"
        PLYMOUTH_PLUGIN_PATH=${PLYMOUTH_LIBDIR}/plymouth/
    fi
fi

function usage ()
{
  echo "usage: plymouth-set-default-theme { --list | --reset [ --rebuild-initrd ] | <theme-name> [ --rebuild-initrd ] | --rebuild-initrd | --help }"
}

function show_help() {
  cat <<EOF
Plymouth theme chooser
$(usage)

  -h, --help             Show this help message
  -l, --list             Show available themes
  -r. --reset            Reset to default theme
  -R, --rebuild-initrd   Rebuild initrd (necessary after changing theme)
  <theme-name>           Name of new theme to use (see --list for available themes)

EOF
}

function list_themes ()
{
        for theme in ${PLYMOUTH_DATADIR}/plymouth/themes/*/*.plymouth; do
                [ -f $theme ] || continue;
                echo "$(basename "$theme" .plymouth)"
        done
}

function read_or_set_file_ini_value ()
{
        CONFIG_FILE_PATH="$1"
        CONFIG_FILE_INI_SEGMENT="$2"
        CONFIG_FILE_VALUE_NAME="$3"
        CONFIG_FILE_VALUE_NEW_VALUE="$4"

        unset CONFIG_FILE_LINES_ARRAY

        if [[ ! -e "$CONFIG_FILE_PATH" && ! -z $CONFIG_FILE_VALUE_NEW_VALUE ]]; then
                echo > "$CONFIG_FILE_PATH"
        else
                if [[ ! -e "$CONFIG_FILE_PATH" ]]; then
                        return
                fi
        fi

        #read the config file
        CONFIG_FILE_ARRAY=$(<"$CONFIG_FILE_PATH")

        #Convert the file data to an array
        readarray -t CONFIG_FILE_ARRAY <<< "$CONFIG_FILE_ARRAY"

        #Build the initial array for the NeedsFile list
        CONFIG_FILE_LINES_ARRAY=()

        #Handle all the valid lines in the file
        PARSE_INI_SEGMENT=0

        INI_SEGMENT_LINE_NUMBER=0
        CONFIG_VALUE_LINE_NUMBER=0

        CONFIG_FILE_LINE_COUNT=${#CONFIG_FILE_ARRAY[@]}

        for (( CONFIG_FILE_LINE_NUMBER=0 ; CONFIG_FILE_LINE_NUMBER < $CONFIG_FILE_LINE_COUNT ; CONFIG_FILE_LINE_NUMBER++ )); do
                CONFIG_FILE_LINE=${CONFIG_FILE_ARRAY[$CONFIG_FILE_LINE_NUMBER]}

                #Remove whitespace
                CONFIG_FILE_LINE="${CONFIG_FILE_LINE##*( )}"
                CONFIG_FILE_LINE="${CONFIG_FILE_LINE%%*( )}"

                #Remove comments
                CONFIG_FILE_LINE=${CONFIG_FILE_LINE%%#*}

                if [[ $PARSE_INI_SEGMENT == 1 && $CONFIG_FILE_LINE =~ ^[[:space:]]*"[" ]]; then
                        PARSE_INI_SEGMENT=0
                fi

                if [[ $CONFIG_FILE_LINE == "[$CONFIG_FILE_INI_SEGMENT]" ]]; then
                        if [[ $INI_SEGMENT_LINE_NUMBER == 0 ]]; then
                                INI_SEGMENT_LINE_NUMBER=$(( CONFIG_FILE_LINE_NUMBER + 1 ))
                        fi
                        PARSE_INI_SEGMENT=1
                        continue
                fi
                if [[ $PARSE_INI_SEGMENT == 0 ]]; then
                        continue
                fi

                KEY_NAME=""
                KEY_VALUE=""

                CONFIG_FILE_LINE_LAST_CHAR=${CONFIG_FILE_LINE:$((${#CONFIG_FILE_LINE} -1 )):1}
                IFS="="
                CONFIG_FILE_LINE=($CONFIG_FILE_LINE)
                unset IFS

                KEY_NAME=${CONFIG_FILE_LINE[0]}
                KEY_NAME="${KEY_NAME##*( )}"
                KEY_NAME="${KEY_NAME%%*( )}"

                KEY_VALUE=${CONFIG_FILE_LINE[1]}
                KEY_VALUE="${KEY_VALUE##*( )}"
                KEY_VALUE="${KEY_VALUE%%*( )}"

                if [[ "$KEY_NAME" == "$CONFIG_FILE_VALUE_NAME" ]]; then
                        if [[ $CONFIG_VALUE_LINE_NUMBER == 0 ]]; then
                                CONFIG_VALUE_LINE_NUMBER=$(( CONFIG_FILE_LINE_NUMBER + 1 ))
                        fi

                        if [[ -z "$CONFIG_FILE_VALUE_NEW_VALUE" ]]; then
                                echo "$KEY_VALUE"
                                break
                        fi
                fi

        done

        if [[ ! -z $CONFIG_FILE_VALUE_NEW_VALUE ]]; then
                if [[ $INI_SEGMENT_LINE_NUMBER == 0 ]]; then
                        INI_SEGMENT_LINE_NUMBER=1
                        sed -i "$INI_SEGMENT_LINE_NUMBER i [$CONFIG_FILE_INI_SEGMENT]" "$CONFIG_FILE_PATH"
                fi

                if [[ $CONFIG_VALUE_LINE_NUMBER == 0 ]]; then
                        CONFIG_FILE_LINE_COUNT=$(( $INI_SEGMENT_LINE_NUMBER + 1 ))
                        sed -i "$CONFIG_FILE_LINE_COUNT i $CONFIG_FILE_VALUE_NAME=$CONFIG_FILE_VALUE_NEW_VALUE" "$CONFIG_FILE_PATH"
                else
                        sed -ie "${CONFIG_VALUE_LINE_NUMBER}s/.*/$CONFIG_FILE_VALUE_NAME=$CONFIG_FILE_VALUE_NEW_VALUE/" "$CONFIG_FILE_PATH"
                fi
        fi

        unset CONFIG_FILE_ARRAY
}

function get_default_theme ()
{
        THEME_NAME=$(read_or_set_file_ini_value ${PLYMOUTH_CONFDIR}/plymouthd.conf Daemon Theme)
        if [ -z "$THEME_NAME" -o ! -r "${PLYMOUTH_DATADIR}/plymouth/themes/$THEME_NAME/$THEME_NAME.plymouth" ]; then
                THEME_NAME=$(read_or_set_file_ini_value ${PLYMOUTH_POLICYDIR}/plymouthd.defaults Daemon Theme)
        fi

        if [ -z "$THEME_NAME" -o ! -r "${PLYMOUTH_DATADIR}/plymouth/themes/$THEME_NAME/$THEME_NAME.plymouth" \
             -a -L "${PLYMOUTH_DATADIR}/plymouth/themes/default.plymouth" ]; then
                THEME_NAME=$(basename "$(readlink ${PLYMOUTH_DATADIR}/plymouth/themes/default.plymouth)" .plymouth)
        fi
        if [ -z "$THEME_NAME" -o ! -r "${PLYMOUTH_DATADIR}/plymouth/themes/$THEME_NAME/$THEME_NAME.plymouth" ]; then
                THEME_NAME="text"
        fi

        echo $THEME_NAME
}

DO_RESET=0
DO_INITRD_REBUILD=0
DO_LIST=0
DO_HELP=0
THEME_NAME=""
while [ $# -gt 0 ]; do
        case "$1" in

        -l|--list)
                if [ -n "$THEME_NAME" ]; then
                        echo "You can only specify --list or a theme name, not both" >&2
                        echo $(usage) >&2
                        exit 1
                fi

                if [ $DO_RESET -ne 0 ]; then
                        echo "You can only specify --reset or --list, not both" >&2
                        echo $(usage) >&2
                        exit 1
                fi

                DO_LIST=1
        ;;

        -R|--rebuild-initrd)
                DO_INITRD_REBUILD=1
        ;;

        -r|--reset|default)
                if [ -n "$THEME_NAME" ]; then
                        echo "You can only specify --reset or a theme name, not both" >&2
                        echo $(usage) >&2
                        exit 1
                fi

                if [ $DO_LIST -ne 0 ]; then
                        echo "You can only specify --reset or --list, not both" >&2
                        echo $(usage) >&2
                        exit 1
                fi

                DO_RESET=1
        ;;

        -h|--help)
                DO_HELP=1
        ;;

        -[!-]*-*)
                echo "Short options can't have embedded dashes" >&2
                echo $(usage) >&2
                exit 1
        ;;

        -[!-][!-]*)
                COMPRESSED_OPTIONS="$1"
                shift

                ARGS=($COMPRESSED_OPTIONS $(echo $COMPRESSED_OPTIONS | sed -e 's/^-//' -e 's/[^-]/-& /g') "$@")

                set -- "${ARGS[@]}"
        ;;

        -*)
                echo "Unknown option: '$1'" >&2
                echo $(usage) >&2
                exit 1
        ;;

        *)
                if [ -n "$THEME_NAME" ]; then
                        echo "You can only specify one theme at a time" >&2
                        echo $(usage) >&2
                        exit 1
                fi

                if [ $DO_RESET -ne 0 ]; then
                        echo "You can only specify --reset or a theme name, not both" >&2
                        echo $(usage) >&2
                        exit 1
                fi

                if [ $DO_LIST -ne 0 ]; then
                        echo "You can only specify --list or a theme name, not both" >&2
                        echo $(usage) >&2
                        exit 1
                fi

                THEME_NAME="$1"
        ;;
  esac
  shift
done

if [ $DO_HELP -eq 1 ]; then
        show_help
        exit $?
fi

if [ $DO_LIST -ne 0 ]; then
        list_themes
        exit $?
fi

if [ $DO_RESET -eq 0 ] && [ $DO_INITRD_REBUILD -eq 0 ] && [ -z $THEME_NAME ]; then
        get_default_theme
        exit $?
fi

if [ `id -u` -ne 0 ]; then
        echo "This program must be run as root" >&2
        exit 1
fi

if [ $DO_RESET -ne 0 ]; then
        if [ -f ${PLYMOUTH_CONFDIR}/plymouthd.conf ]; then
                sed -i -e '/^Theme[[:blank:]]*=.*/d' ${PLYMOUTH_CONFDIR}/plymouthd.conf || exit $?
        fi

        [ $DO_INITRD_REBUILD -eq 0 ] && exit 0
fi

if [ $DO_INITRD_REBUILD -ne 0 -a -z $THEME_NAME ]; then
        THEME_NAME=$(get_default_theme)
fi

if [ ! -e ${PLYMOUTH_DATADIR}/plymouth/themes/${THEME_NAME}/${THEME_NAME}.plymouth ]; then
        echo "${PLYMOUTH_DATADIR}/plymouth/themes/${THEME_NAME}/${THEME_NAME}.plymouth does not exist" >&2
        exit 1
fi

MODULE_NAME=$(grep "ModuleName *= *" ${PLYMOUTH_DATADIR}/plymouth/themes/${THEME_NAME}/${THEME_NAME}.plymouth | sed 's/ModuleName *= *//')

if [ ! -e ${PLYMOUTH_PLUGIN_PATH}${MODULE_NAME}.so ]; then
        echo "${PLYMOUTH_PLUGIN_PATH}${MODULE_NAME}.so does not exist" >&2
        exit 1
fi

[ -L ${PLYMOUTH_DATADIR}/plymouth/themes/default.plymouth ] && rm -f ${PLYMOUTH_DATADIR}/plymouth/themes/default.plymouth

[ -d ${PLYMOUTH_CONFDIR} ] || mkdir -p ${PLYMOUTH_CONFDIR}
read_or_set_file_ini_value ${PLYMOUTH_CONFDIR}/plymouthd.conf Daemon Theme ${THEME_NAME}

[ $DO_INITRD_REBUILD -ne 0 ] && (${PLYMOUTH_LIBEXECDIR}/plymouth/plymouth-update-initrd)
exit 0
